﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using ManyinEngine;

namespace TestViewer
{
    public partial class SemiCanvas : Panel
    {
        private List<Tile> _lstMap;

        public List<Tile> LstMapa
        {
            get
            {
                if (_lstMap == null)
                {
                    _lstMap = new List<Tile>();
                }
                return _lstMap;
            }
            set { _lstMap = value; }
        }

        public Color BackgroundColor { get; set; }

        public Color BorderColor { get; set; }

        public Color TileColor { get; set; }

        public SemiCanvas()
        {
            LstMapa = new List<Tile>();
        }


        protected override void OnPaint(PaintEventArgs e)
        {
            //var myPen = new Pen(Color.Red);
            //e.Graphics.DrawLine(myPen, 0, 0, 200, 200);
            //myPen.Dispose();

            foreach (var tile in LstMapa)
            {
                PaintTile(tile, e.Graphics);
            }
            
            //e.Graphics.TranslateTransform(AutoScrollPosition.X, AutoScrollPosition.Y);
            base.OnPaint(e);
        }

        private PointF[] GetInnerPointFsFromNode(Tile tile)
        {
            var ret = new PointF[tile.Sides];
            var a = (Math.PI * 2) / tile.Sides;
            ret[0] = new PointF(tile.CenterX + tile.Radius - tile.BorderThickness, tile.CenterY + 0);
            for (int i = 1; i < tile.Sides; i++)
            {
                ret[i] = new PointF(tile.CenterX + Convert.ToSingle((tile.Radius - tile.BorderThickness) * Math.Cos(a * i)), tile.CenterY + Convert.ToSingle((tile.Radius - tile.BorderThickness) * Math.Sin(a * i)));
            }
            return ret;
        }

        private PointF[] GetOuterPointFsFromNode(Tile tile)
        {
            var ret = new PointF[tile.Sides];
            var a = (Math.PI * 2) / tile.Sides;
            ret[0] = new PointF(tile.CenterX + tile.Radius, tile.CenterY + 0);
            for (int i = 1; i < tile.Sides; i++)
            {
                ret[i] = new PointF(tile.CenterX + Convert.ToSingle((tile.Radius) * Math.Cos(a * i)), tile.CenterY + Convert.ToSingle((tile.Radius) * Math.Sin(a * i)));
            }
            return ret;
        }

        private void PaintTile(Tile tile, Graphics graphics)
        {
            // drow hex from x;y
            tile.CenterY *= -1;

            tile.CenterY += Height / 2;
            tile.CenterX += Width / 2;

            
            var points = GetOuterPointFsFromNode(tile);
            var brush = new SolidBrush(BorderColor);

            graphics.FillPolygon(brush, points);

            brush = new SolidBrush(TileColor);
            

            points = GetInnerPointFsFromNode(tile);
            graphics.FillPolygon(brush, points);

            brush = new SolidBrush(Color.Black);
            graphics.DrawString(tile.Node.Index.ToString(), new Font(FontFamily.GenericMonospace, 12f), brush,
                tile.CenterX - 10, tile.CenterY - 5);
        }
    }
}
